package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.Comment;
import com.rain.shoppingscienceproj.module.ImageData;
import com.rain.shoppingscienceproj.ui.view.MyGridView;
import com.rain.shoppingscienceproj.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder> {

    private List<Comment> list;
    private Context mContext;

    public CommentAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<Comment> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment_layout, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Comment comment = list.get(position);
        if (null != comment.getAvatarurl() || !"".equals(comment.getAvatarurl())) {
            ImageUtils.imageCircle(mContext, comment.getAvatarurl(), holder.iv_photo_user);
        }
        holder.tv_user_name.setText(comment.getNickname() + "   " + comment.getCtime());
        holder.tv_content.setText(comment.getContext());
        if (comment.getContextreply() == null || comment.equals("")) {
            holder.tv_store_context.setVisibility(View.GONE);
        } else {
            holder.tv_store_context.setVisibility(View.VISIBLE);
            holder.tv_store_context.setText("商家回复: " + comment.getContextreply());
        }
        if (comment.getImgs().size() != 0) {
            ImageAdapter adapter = new ImageAdapter(mContext);
            holder.gv_data.setAdapter(adapter);
            List<ImageData> list = new ArrayList<>();
            for (int i = 0; i < comment.getImgs().size(); i++) {
                ImageData imageData = new ImageData(comment.getImgs().get(i), false);
                list.add(imageData);
            }
            adapter.setImages(list);
        }
    }

    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        else
            return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_photo_user)
        ImageView iv_photo_user;
        @BindView(R.id.tv_user_name)
        TextView tv_user_name;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.gv_data)
        MyGridView gv_data;
        @BindView(R.id.tv_store_context)
        TextView tv_store_context;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
